﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsRemoteScripting.Action
{
    public class Save : AbstractCommand
    {
        private string m_sFileName = string.Empty;

        public Save()
        {
        }

        public Save(string _sFileName)
        {
            this.m_sFileName = _sFileName;
        }

        private bool m_bUseFileDialog = true;

        public override void Run(object sender, EventArgs e)
        {
            string sFileName = string.Empty;
            SaveFileDialog fd = new SaveFileDialog();
            if (UseFileDialog)
            {
                if (fd.ShowDialog() != DialogResult.OK) return;
                sFileName = fd.FileName;
            }
            else
            {
                sFileName = m_sFileName;
            }

            string sText = Form1.m_Form.textBoxScript.Text;
            if (string.IsNullOrEmpty(sText)) return;
            File.WriteAllText(sFileName, sText);
        }

        public string FileName
        {
            get { return m_sFileName; }
            set { m_sFileName = value; }
        }

        public bool UseFileDialog
        {
            get { return m_bUseFileDialog; }
            set { m_bUseFileDialog = value; }
        }
    }
}
